package com.briup.ch08;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * @Author briup-adam
 * @Date 2023/10/12 下午6:02
 * @Description 测试不同数据结构做相同操作的耗时情况
 **/

public class ListTimeCompare {
//    Array数组实现
    static List a=new ArrayList();
//    Linked双向链表实现
    static List l=new LinkedList();
    //查找
    static  void  find(){
        long start = System.currentTimeMillis();
        for (int i = 0; i <100000 ; i++) {
            a.get(new Random().nextInt(a.size()));
        }
        long end = System.currentTimeMillis();
        System.out.println("arrayList查找1w次元素耗时"+(end-start));
         start = System.currentTimeMillis();
        for (int i = 0; i <100000 ; i++) {
            l.get(new Random().nextInt(l.size()));
        }
         end = System.currentTimeMillis();
        System.out.println("linkedList查找1w次元素耗时"+(end-start));

    }
    //插入元素
    static void  insert(){
        //获取当前系统时间
        long start = System.currentTimeMillis();
        for (int i = 0; i <100000 ; i++) {
            a.add(0,"数组");
        }
        long end = System.currentTimeMillis();
        System.out.println("arrayList10w个元素插入耗时"+(end-start));
         start = System.currentTimeMillis();
        for (int i = 0; i <100000 ; i++) {
            l.add(0,"数组");
        }
         end = System.currentTimeMillis();
        System.out.println("linkedList10w个元素插入耗时"+(end-start));

    }
    public static void main(String[] args) {
        insert();
        find();
    }
}
